import request from '../utils/request';
import code from '../utils/k';

export async function fetchVipMyAcc(card_id, card_no, phone, shopId) {
     return await request('/findVipCardNo', {params: {card_id, card_no, phone, shopId}})
}

export async function fetchVipCardBranch(shopId, branch_no, showAll = true) {
  const param = { shopId, showAll };
  if (branch_no) {
    param.branch_no = branch_no;
  }
  return await request('/vipCardBranch', {
    params: {
      filter: JSON.stringify(param),
    },
  });
}

export async function getWheelStatus(shopId, card_id) {
  return await request("/bigWheel/" + shopId, {
    params: {
      card_id: card_id
    },
    method: "get"
  });
}

export async function fetch(page = 1, pageSize = 20, filter = {}, sort) {
  return await request('/vips', {
    params: {
      page,
      pageSize,
      filter: JSON.stringify(filter),
      sort: sort && JSON.stringify(sort),
    },
  });
}

export async function fetchVisitedRecord(shopId, salemanId, card_no) {
  return await request(`/visitRecds`, {
    params: { shopId, salemanId, card_no },
  });
}

export async function editVisitRecds(id, remark) {
  return await request(`/visitRecds/${id}`, {
    method:'put',
    body: { remark },
  });
}

export async function fetchVipInfo(shopId, card_id) {
  return await request(`/shop/${shopId}/vip/${encodeURIComponent(card_id)}`);
}

export async function fetchVipConsume(shopId, card_id) {
  return await request(`/shop/${shopId}/vip/${card_id}/consume`);
}
export async function fetchVipAcc(shopId, cardNo) {
  return await request(`/shop/${shopId}/vip/${cardNo}/acc`);
}

export function findVipCardNo(shopId, phone, card_no, card_id, code, saleman_id, debugMode) {
  const params = { shopId, phone, code, saleman_id, card_no, card_id };
  if (debugMode) {
    params.__debug = true;
  }
  return request('/findVipCardNo', {
    params,
  });
}

export function fetchVerifyCode(phone, shopId, data = {}) {
  data.type = data.type || '3';
  return request('/smsVerifyCode', {
    method: 'post', body: { d: code({ phone, type: data.type, shopId, reg: data.reg, bind: data.bind }) }, // type - 验证码类型：3 电子会员卡, 4 新增会员, 5 会员积分
  });
}

/* 置顶*/
export async function updateStick(_id, is_stick) {
  return await request(`/vips/${_id}`, { method: 'put', body: { is_stick } });
}


export async function updateRemarks(_id, remarks) {
  return await request(`/vips/${_id}`, { method: 'put', body: { remarks } });
}

export async function create(newVip) {
  return await request('/createVip', { method: 'post', body: newVip });
}
export async function reg(newVip) {
  return await request('/reg', { method: 'post', body: newVip });
}

export async function fetchVipCardInfo(shopId) {
  return await request(`/shop/${shopId}/vipCard`);
}

export async function vipEdit(data) {
  return await request(`/vipEdit`, { method: 'post', body: data });
}

export async function bindVipCard(data) {
  return await request(`/bindVipCard`, { method: 'post', body: data });
}

//获取会员标签
export async function fetchVipLabel(shopId,card_id) {
  return await request(`/shop/${shopId}/vip/${card_id}/tag`);
}

//获取所有会员标签
export async function fetchShopLabel(shopId) {
  return await request(`/shop/${shopId}/tag`);
}

//添加会员标签
export async function editVipLabel(shopId, card_id, data) {
  return await request(`/shop/${shopId}/vip/${card_id}/tag/all`, { method: 'put', body: data });
}


export async function fetchCoupon(shopId, data) {
  return await request(`/shop/${shopId}/coupon`, {
    params: {
      ...data,
      filter: JSON.stringify({ status: { $in: [1, 2, 3] }, ...data.filter }),
    },
  });
}

export async function fetchCouponCode(shopId, data) {
  return await request(`/shop/${shopId}/couponCode`, {
    params: {
      ...data,
      filter: JSON.stringify({ status: { $in: [1, 2, 3] }, ...data.filter }),
    },
  });
}

export async function fetchVipTypes(shopId, data = {}) {
  return await request(`/shop/${shopId}/vipType`, {
    params: {
      ...data,
      filter: JSON.stringify({ ...(data.filter || {}) }),
    },
  });
}

export async function fetchShopWechat(shopId) {
  return await request(`/shopWechat/${shopId}`);
}

export async function fetchOpenId(shopId, data) {
  return await request(`/shop/${shopId}/openid`, { method: 'post', body: data });
}

export async function fetchShopInfo(shopId) {
  return await request(`/shop`, {
    params: {
      filter: JSON.stringify({ ...({ shopId }) }),
    },
  });
}

export async function fetchJc(shopId, cardNo) {
  return await request(`/shop/${shopId}/vip/${cardNo}/jc`);
}

export async function fetchSavelist(shopId, cardNo) {
  return await request(`/shop/${shopId}/vip/${cardNo}/savelist`);
}

export async function fetchStored(shopId, cardNo) {
  return await request(`/shop/${shopId}/vip/${cardNo}/stored`);
}

export async function createConsumeFeedback(shopId, data) {
  console.log(data);
  return await request(`/shop/${shopId}/consumeFeedback`, { method: 'post', body: data });
}

export function findVipByFP(shopId, fingerprint) {
  return request('/findVipByFP', {
    params: { shopId, fingerprint },
  });
}

export function getOpenidByCode(code, shopId) {
  return request(`/findVipByOpenid/${code}`, {
    params: { shopId }
  })
}

export async function findVipByOpenid(openid, shopId) {
  return await request('/findVipByOpenid', {
    params: {
      openid,
      shopId,
    }
  });
}

export async function loginByOpenid(openid) {
  return await request(`/loginByOpenid`, { method: 'post', body: { openid } });
}

export async function bindUserOpenid(openid) {
  return await request(`/bindUserOpenid`, { method: 'post', body: { openid } });
}

export async function fetchPaidVip(shopId) {
  return await request(`/shop/${shopId}/paidVip`);
}

export async function createPaidVipOrder(shopId, data) {
  return await request(`/shop/${shopId}/paidVipOrder`, { method: 'post', body: data });
}

export async function fetchPayOrder(id) {
  return await request(`/payOrder/${id}`);
}

export async function fetchPayOrderList(data = {}) {
  return await request(`/payOrder`, {
    params: {
      filter: JSON.stringify(data),
    },
  });
}

export async function fetchSyncOnline(shopId) {
  return await request(`/syncOnline`, {
    params: {
      filter: JSON.stringify({ ...({ shopId }) }),
    },
  });
}

export async function fetchVipType(shopId) {
  return await request(`/shop/${shopId}/vipType`, {
    params: {
      filter: JSON.stringify({ ...({ shopId }) }),
    },
  });
}
//http://wx.hy-bang.com/api/shop/sixun_000153/vipCard
export async function fetchVipCard(shopId) {
  return await request(`/shop/${shopId}/vipCard`, {
    params: {
      filter: JSON.stringify({ ...({ shopId }) }),
    },
  });
}
// 积分兑换 shop/sixun_000153/accCouponConfig?includeCoupon=true&pageSize=100
export async function fetchConvertAcc(shopId, pageSize) {
  return await request(`/shop/${shopId}/accCouponConfig`, {
    params: {
      filter: JSON.stringify({ ...({ shopId }) }),
      includeCoupon: true,
      pageSize
    },
  });
}

// 消费记录 http://admm.ymlgz.cn:3000/mock/6/api/shop:shopId/vip/:card_id/consum
export async function fetchConsume(shopId, card_id) {
  return await request(`/v6/shop/${shopId}/vip/${card_id}/consume`, {
    params: {
      filter: JSON.stringify({ ...({ shopId, card_id}) }),
    },
  });
}

// http://wx.hy-bang.com/api//findVipCardNo?
//card_id=18476525018&card_no=18476525018&phone=18476525018&shopId=sixun_000153
export async function fetchVipInfo2(shopId, card_id, card_no, phone) {
  return await request(`/v6/findVipCardNo`, {
    params: {
      shopId, card_id, card_no, phone
    },
  });
}

// 皮肤选项
export async function fetchTags(shopId) {
  return await request(`/v6/tagsFetch`, {
    params: {
      filter: JSON.stringify({ ...({ shopId }) }),
    },
  });
}

// updateProfile 修改会员资料
export async function updateProfile(shopId, card_id, data) {
  return await request(`/shop/${shopId}/vip/${card_id}/updateProfile`, {
    method: 'post', body: data
  });
}

// http://wx.hy-bang.com/api/shop/sixun_000153/branch/0004/saleMan?filterByUser=true
export async function fetchSaleMan(shopId, branchId) {
  return await request(`/shop/${shopId}/branch/${branchId}/saleMan`, {
    params: {
      filter: JSON.stringify({ ...({ shopId,branchId, filterByUser:true }) }),
    },
  });
}

export async function regNew(newVip) {
  return await request('/v6/createVip', { method: 'post', body: newVip });
}